<template>
  <div class="settings-page">
    <el-row :gutter="20">
      <el-col :span="16">
        <el-card title="系统设置">
          <template #header>
            <span>系统设置</span>
          </template>
          
          <el-form
            ref="settingsFormRef"
            :model="settingsForm"
            :rules="settingsRules"
            label-width="120px"
          >
            <el-form-item label="系统名称" prop="systemName">
              <el-input v-model="settingsForm.systemName" />
            </el-form-item>
            
            <el-form-item label="系统描述" prop="systemDesc">
              <el-input
                v-model="settingsForm.systemDesc"
                type="textarea"
                :rows="3"
              />
            </el-form-item>
            
            <el-form-item label="系统版本" prop="systemVersion">
              <el-input v-model="settingsForm.systemVersion" readonly />
            </el-form-item>
            
            <el-form-item label="维护模式">
              <el-switch
                v-model="settingsForm.maintenanceMode"
                active-text="开启"
                inactive-text="关闭"
              />
            </el-form-item>
            
            <el-form-item label="用户注册">
              <el-switch
                v-model="settingsForm.allowRegister"
                active-text="允许"
                inactive-text="禁止"
              />
            </el-form-item>
            
            <el-form-item label="邮件通知">
              <el-switch
                v-model="settingsForm.emailNotification"
                active-text="开启"
                inactive-text="关闭"
              />
            </el-form-item>
            
            <el-form-item label="数据备份">
              <el-radio-group v-model="settingsForm.backupFrequency">
                <el-radio label="daily">每日</el-radio>
                <el-radio label="weekly">每周</el-radio>
                <el-radio label="monthly">每月</el-radio>
              </el-radio-group>
            </el-form-item>
            
            <el-form-item>
              <el-button type="primary" @click="handleSave">保存设置</el-button>
              <el-button @click="handleReset">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>
      
      <el-col :span="8">
        <el-card title="系统信息">
          <template #header>
            <span>系统信息</span>
          </template>
          
          <div class="system-info">
            <div class="info-item">
              <span class="info-label">服务器时间:</span>
              <span class="info-value">{{ currentTime }}</span>
            </div>
            <div class="info-item">
              <span class="info-label">运行时间:</span>
              <span class="info-value">7天 12小时 30分钟</span>
            </div>
            <div class="info-item">
              <span class="info-label">CPU使用率:</span>
              <span class="info-value">
                <el-progress :percentage="45" :stroke-width="8" />
              </span>
            </div>
            <div class="info-item">
              <span class="info-label">内存使用率:</span>
              <span class="info-value">
                <el-progress :percentage="68" :stroke-width="8" status="warning" />
              </span>
            </div>
            <div class="info-item">
              <span class="info-label">磁盘使用率:</span>
              <span class="info-value">
                <el-progress :percentage="32" :stroke-width="8" status="success" />
              </span>
            </div>
          </div>
        </el-card>
        
        <el-card title="操作日志" style="margin-top: 20px;">
          <template #header>
            <span>最近操作</span>
          </template>
          
          <el-timeline size="small">
            <el-timeline-item
              v-for="(log, index) in operationLogs"
              :key="index"
              :timestamp="log.time"
              size="small"
            >
              {{ log.action }}
            </el-timeline-item>
          </el-timeline>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, onUnmounted } from 'vue'
import { ElMessage } from 'element-plus'

const settingsFormRef = ref()
const currentTime = ref('')
let timeInterval = null

const settingsForm = reactive({
  systemName: '后台管理系统',
  systemDesc: '基于Vue3的现代化管理平台',
  systemVersion: 'v1.0.0',
  maintenanceMode: false,
  allowRegister: true,
  emailNotification: true,
  backupFrequency: 'daily'
})

const settingsRules = {
  systemName: [
    { required: true, message: '请输入系统名称', trigger: 'blur' }
  ],
  systemDesc: [
    { required: true, message: '请输入系统描述', trigger: 'blur' }
  ]
}

const operationLogs = ref([
  {
    action: '管理员修改了系统设置',
    time: '2024-01-20 14:30:00'
  },
  {
    action: '用户 张三 登录系统',
    time: '2024-01-20 14:25:00'
  },
  {
    action: '系统自动备份完成',
    time: '2024-01-20 14:00:00'
  },
  {
    action: '新用户 李四 注册',
    time: '2024-01-20 13:45:00'
  }
])

const updateTime = () => {
  currentTime.value = new Date().toLocaleString('zh-CN')
}

const handleSave = async () => {
  if (!settingsFormRef.value) return
  
  try {
    await settingsFormRef.value.validate()
    ElMessage.success('设置保存成功')
  } catch (error) {
    console.error('表单验证失败:', error)
  }
}

const handleReset = () => {
  if (settingsFormRef.value) {
    settingsFormRef.value.resetFields()
  }
}

onMounted(() => {
  updateTime()
  timeInterval = setInterval(updateTime, 1000)
})

onUnmounted(() => {
  if (timeInterval) {
    clearInterval(timeInterval)
  }
})
</script>

<style scoped>
.settings-page {
  padding: 20px;
}

.system-info {
  .info-item {
    display: flex;
    align-items: center;
    margin-bottom: 16px;
    
    .info-label {
      width: 80px;
      color: #666;
      font-size: 14px;
    }
    
    .info-value {
      flex: 1;
      font-size: 14px;
    }
  }
}
</style>